/**
 * 
 */
package com.hw.hcit.feature;
import com.hw.hcit.R;
import com.hw.hcit.conf.Config;
import com.hw.hcit.conf.JCtrlCallback;
import com.hw.hcit.conf.JTestItem;

import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * @author gaoshengpan
 *
 */
public class VibrationTestItem extends JTestItem {
    private TextView textView ;
    private Vibrator vibrator;
    private boolean tested = false;
    private boolean powerClicked = false;
    private String TAG = "VibrationTestItem";
    private Button mButton1,mButton2;
    private Handler mHandler =new Handler(){
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            tested = true;
        }
    };

    /**
     * @param cb
     */
    public VibrationTestItem(JCtrlCallback cb) {
        super(cb);
        // TODO Auto-generated constructor stub
    }

    /**
     * 
     */
    public VibrationTestItem() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        vibrator = (Vibrator) Config.GetActivity().getApplicationContext().getSystemService("vibrator");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View rootView = inflater.inflate(R.layout.vibration_test, container, false);
        textView = (TextView) rootView.findViewById(R.id.textView1);
        mButton1 = (Button) rootView.findViewById(R.id.button1);
		mButton2 = (Button) rootView.findViewById(R.id.button2);
		mButton1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(Config.CURR_PROJECT.isAuto()){
					if (!Config.ageingTested) {
						rewriteOneByteOfNv2499(12, 'P');
					} else {
						rewriteOneByteOfNv2499(43, 'P');
					}
				}
				 testFinish(true, 1, true);
				
			}
		});
		mButton2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(Config.CURR_PROJECT.isAuto()){
					if (!Config.ageingTested) {
						rewriteOneByteOfNv2499(12, 'F');
					} else {
						rewriteOneByteOfNv2499(43, 'F');
					}
				}
				testFinish(false, 1, false);
				
			}
		});
        return rootView;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        tested = false;
        powerClicked = false;
        long al[] = { 0,1000L,1000l, 1000l,1000l };
        if(vibrator == null){
            vibrator = (Vibrator) Config.GetActivity().getApplicationContext().getSystemService("vibrator");
        }
        vibrator.vibrate(al, 0);
        mHandler.sendEmptyMessageDelayed(0, 600);
        
    }

    @Override
    public void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        if(!(vibrator == null))
        {
            vibrator.cancel();
            vibrator=null;
        }
    }
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        return true;
    }
   /* @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
             dissmissPowerClicked();
            Log.d(TAG, " --------->KeyEvent.KEYCODE_VOLUME_DOWN");
            if (powerClicked) {
                Log.d(TAG,
                        "powerClicked --------->KeyEvent.KEYCODE_VOLUME_DOWN");
                testFinish(false, 1, false);
                return true;
            }
            if (tested) {
                Log.d(TAG,
                        "tested --------->KeyEvent.KEYCODE_VOLUME_DOWN");
                testFinish(true, 1, true);
            } else {
                Log.d(TAG, " --------->showTestUncomplete");
                showTestUncomplete();
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
            dissmissPowerClicked();
            testFinish(true, -1, true);
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
            Log.d(TAG, "powerClicked --------->KeyEvent.KEYCODE_POWER");
            powerClicked = true;
            showPowerClicked();
        }
        return true;
    }*/

}
